package flc.ast.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.fliter.PhotoProcessing;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.PictureFilterAdapter;
import flc.ast.bean.MyPictureFilterBean;
import flc.ast.databinding.FragmentFilterBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class FilterFragment extends BaseEditFragment<FragmentFilterBinding> {

    private PictureFilterAdapter mPictureFilterAdapter;
    private Bitmap mFilterBitmap;// 滤镜处理后的bitmap
    private boolean mHasOriginal;
    private int oldPosition;

    public static FilterFragment newInstance() {
        FilterFragment fragment = new FilterFragment();
        return fragment;
    }


    @Override
    public void onShow() {
        mSingleResultActivity.mode = ImgEditActivity.MODE_FILTER;
        if (mSingleResultActivity.mHasOriginFilter) {
            mSingleResultActivity.mImgView.setImageBitmap(mSingleResultActivity.getmNoFilterEditBitmap());
        } else {
            mSingleResultActivity.mImgView.setImageBitmap(mSingleResultActivity.getmHasFilterBitmap());
        }
    }

    @Override
    public void backToMain() {
        mSingleResultActivity.mImgView.setVisibility(View.VISIBLE);
        if (mSingleResultActivity.mHasOriginFilter) {
            mSingleResultActivity.mImgView.setImageBitmap(mSingleResultActivity.getmNoFilterEditBitmap());// 返回原图
        } else {
            mSingleResultActivity.mImgView.setImageBitmap(mSingleResultActivity.getmHasFilterBitmap());// 返回原图
        }
        mSingleResultActivity.mode = ImgEditActivity.MODE_NONE;
        mSingleResultActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_filter;
    }

    @Override
    protected void initView() {
        mHasOriginal = mSingleResultActivity.mHasOriginFilter;
        mSingleResultActivity.mode = ImgEditActivity.MODE_FILTER;

        mDataBinding.rvFilter.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mPictureFilterAdapter = new PictureFilterAdapter();
        mDataBinding.rvFilter.setAdapter(mPictureFilterAdapter);
        mPictureFilterAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mPictureFilterAdapter.getItem(oldPosition).setSelect(false);
        mPictureFilterAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        mPictureFilterAdapter.getItem(position).setSelect(true);
        mPictureFilterAdapter.notifyItemChanged(position);
        setFilter(position);
    }

    @Override
    protected void initData() {
        //滤镜数据
        String[] mFilters = getResources().getStringArray(R.array.filters);
        List<MyPictureFilterBean> listFilter = new ArrayList<>();
        listFilter.add(new MyPictureFilterBean(mFilters[0], "#00000000", true));
        listFilter.add(new MyPictureFilterBean(mFilters[1], "#323333", false));
        listFilter.add(new MyPictureFilterBean(mFilters[2], "#C376F3", false));
        listFilter.add(new MyPictureFilterBean(mFilters[3], "#6F7171", false));
        listFilter.add(new MyPictureFilterBean(mFilters[4], "#505151", false));
        listFilter.add(new MyPictureFilterBean(mFilters[5], "#999999", false));
        listFilter.add(new MyPictureFilterBean(mFilters[6], "#513232", false));
        listFilter.add(new MyPictureFilterBean(mFilters[7], "#787A7A", false));
        listFilter.add(new MyPictureFilterBean(mFilters[8], "#5000FF00", false));
        listFilter.add(new MyPictureFilterBean(mFilters[9], "#500000FF", false));
        listFilter.add(new MyPictureFilterBean(mFilters[10], "#50FF0000", false));
        listFilter.add(new MyPictureFilterBean(mFilters[11], "#50FFFF00", false));
        mPictureFilterAdapter.setList(listFilter);
    }

    private void setFilter(final int f) {
        showDialog(getString(com.stark.common.res.R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap retBitmap;
                retBitmap = Bitmap.createBitmap(mSingleResultActivity.getmNoFilterEditBitmap().copy(Bitmap.Config.ARGB_8888, true));
                PhotoProcessing.filterPhoto(retBitmap, f);
                emitter.onNext(retBitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) {
                    return;
                }
                mFilterBitmap = bitmap;
                mSingleResultActivity.mImgView.setImageBitmap(mFilterBitmap);
            }
        });
    }

    public void applyFilterImage() {
        if (mFilterBitmap != null && mFilterBitmap != mSingleResultActivity.getMainBit()) {
            mSingleResultActivity.changeMainBitmap(mSingleResultActivity.getmNoFilterEditBitmap(), mFilterBitmap, true);
            mSingleResultActivity.mHasOriginFilter = mHasOriginal;
        }
        backToMain();
    }

}